package com.eduadmin.controller;

import com.alibaba.excel.EasyExcel;
import com.eduadmin.entity.Exam;
import com.eduadmin.entity.Grade;
import com.eduadmin.entity.Page;
import com.eduadmin.entity.Student;
import com.eduadmin.listener.ExamDataUploadListener;
import com.eduadmin.listener.GradeDataUploadListener;
import com.eduadmin.service.IExamService;
import com.eduadmin.service.IGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ExamController {
    //构造函数自动装配
    private final IExamService examService;

    @Autowired
    public ExamController(IExamService examService) {
        this.examService = examService;
    }

    //新增
    @RequestMapping(value = "newAddExam")
    @ResponseBody
    public Map<String, String> newAddExam(Exam exam, HttpServletRequest request) {
        Map<String, String> ret = new HashMap<>();

        Exam ea = examService.getExam(exam);
        if (ea != null) {
            //已经在数据库中
            ret.put("type", "error");
            ret.put("msg", "增加失败，该年级该学期的考试名称已经在系统中！");
            return ret;
        }
        int n = examService.newAddExam(exam);
        if (1 != n) {
            //新增失败
            ret.put("type", "error");
            ret.put("msg", "增加失败！");
        } else {
            //新增成功
            List<Exam> examList = examService.getAllExam();
            request.getSession().setAttribute("examList", examList);
            ret.put("type", "success");
            ret.put("msg", "增加成功！");
        }
        return ret;
    }

    //编辑 hgm
    @RequestMapping(value = "editExam")
    @ResponseBody
    public Map<String, String> editExam(Exam exam, HttpServletRequest request) {
        Map<String, String> ret = new HashMap<>();
        Exam ea = examService.getExam(exam);
        if (ea == null) {
            //实际上执行的插入操作
            examService.newAddExam(exam);
            ret.put("type", "success");
            ret.put("msg", "增加成功！");
        } else {
            //更新
            if(exam.getExamId() == ea.getExamId()){
                examService.updateExam(exam);
                ret.put("type", "success");
                ret.put("msg", "修改信息成功！");
            }else {
                ret.put("type", "error");
                ret.put("msg", "修改失败，考试名称已经在系统中！");
            }
        }
        List<Exam> examList = examService.getAllExam();
        request.getSession().setAttribute("examList", examList);
        return ret;
    }

    //删除 hgm
    @RequestMapping(value = "deleteExam")
    @ResponseBody
    public Map<String, String> deleteExam(@RequestParam(value = "ids[]") String[] examidArray,HttpServletRequest request) {
        //拼接为 ，分割的字符串 方便数据库删除操作
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < examidArray.length; i++) {
            sb.append("'" + examidArray[i] + "'");
            if (i < examidArray.length - 1) {
                sb.append(",");
            }
        }
        String examIdString = sb.toString();
        Map<String, String> ret = new HashMap<>();
        int n = examService.deleteExam(examIdString);
        if (n != 0) {
            ret.put("type", "success");
            ret.put("msg", "删除成功！");
            List<Exam> examList=examService.getAllExam();
            request.getSession().setAttribute("examList",examList);
        } else {
            ret.put("type", "error");
            ret.put("msg", "删除失败！");
        }
        return ret;
    }

    //导入数据
    @RequestMapping(value = "addExams")
    @ResponseBody
    public String addExams(MultipartFile examsFile,HttpServletRequest request) throws IOException {
        EasyExcel.read(examsFile.getInputStream(), Exam.class, new ExamDataUploadListener(examService)).sheet().doRead();
        List<Exam> examList=examService.getAllExam();
        request.getSession().setAttribute("examList",examList);
        return "上传成功";
    }

    //获取所有考试信息
    @RequestMapping(value = "getAllExams")
    @ResponseBody
    public Map<String, Object> getAllExams(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows
    ) {
        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> reMap = new HashMap<>();                 //查询结果map
        Map<String, Object> paraMap = new HashMap<>();           //查询参数map
        paraMap.put("offset", pageBean.getOffset());
        paraMap.put("rows", pageBean.getRows());

        List<Exam> examList = examService.getAllExams(paraMap);
        Long total = examService.getAllTotal(paraMap);
        reMap.put("rows", examList);
        reMap.put("total", total);

        return reMap;
    }

}
